package com.myc.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myc.train.business.enums.SeatColEnum;
import com.myc.train.common.exception.BusinessException;
import com.myc.train.common.exception.BusinessExceptionEnum;
import com.myc.train.common.resp.PageResp;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.business.domain.TrainCarriage;
import com.myc.train.business.domain.TrainCarriageExample;
import com.myc.train.business.mapper.TrainCarriageMapper;
import com.myc.train.business.req.TrainCarriageQueryReq;
import com.myc.train.business.req.TrainCarriageSaveReq;
import com.myc.train.business.resp.TrainCarriageQueryResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainCarriageService {

    @Resource
    private TrainCarriageMapper trainCarriageMapper;

    public void save(TrainCarriageSaveReq req){
        DateTime now = DateTime.now();

        //自动计算出列数和座位数
        List<SeatColEnum> colsByType = SeatColEnum.getColsByType(req.getSeatType());
        req.setColCount(colsByType.size());
        req.setSeatCount( req.getColCount() * req.getRowCount());


        TrainCarriage trainCarriage = BeanUtil.copyProperties(req, TrainCarriage.class);

        if(ObjectUtil.isNull(trainCarriage.getId())){
            //验证唯一键是否为空
            TrainCarriage trainCarriage1 = selectByUnique(req.getTrainCode(), req.getIndex());
            if(ObjectUtil.isNotEmpty(trainCarriage1)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CARRIAGE_INDEX_UNIQUE_ERROR);
            }
            trainCarriage.setId(SnowUtil.getSnowflakeNextId());
            trainCarriage.setCreateTime(now);
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.insert(trainCarriage);
        }else{
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.updateByPrimaryKey(trainCarriage);
        }
    }

    //唯一性校验
    public TrainCarriage selectByUnique(String trainCode , Integer index){
        //定义查询条件
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        trainCarriageExample.createCriteria().andTrainCodeEqualTo(trainCode).andIndexEqualTo(index);
        List<TrainCarriage> trainCarriages = trainCarriageMapper.selectByExample(trainCarriageExample);
        if(CollUtil.isNotEmpty(trainCarriages)){
            return trainCarriages.get(0);
        }else {
            return null;
        }
    }

    public PageResp<TrainCarriageQueryResp> queryList(TrainCarriageQueryReq req){
       //定义查询条件
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        //根据id倒序排序
        trainCarriageExample.setOrderByClause("train_code asc, `index` asc");
        //创建查询对象
        TrainCarriageExample.Criteria criteria = trainCarriageExample.createCriteria();
        //不为空则对其新增查询条件
        if(ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        //新增分页信息
        PageHelper.startPage(req.getPage(),req.getSize());
        //根据条件查询结果
        List<TrainCarriage> trainCarriages = trainCarriageMapper.selectByExample(trainCarriageExample);
        //new一个PageInfo并将结果放进来
        PageInfo<TrainCarriage> trainCarriagePageInfo = new PageInfo<>(trainCarriages);
        //类拷贝
        List<TrainCarriageQueryResp> queryResps = BeanUtil.copyToList(trainCarriages, TrainCarriageQueryResp.class);
        //new一个PageResp
        PageResp<TrainCarriageQueryResp> resp= new PageResp<>();
        //放数据
        resp.setTotal(trainCarriagePageInfo.getTotal());
        resp.setList(queryResps);


        return resp;
    }

    public void delete(Long id){
        trainCarriageMapper.deleteByPrimaryKey(id);
    }

    //获取车厢
    public List<TrainCarriage> selectCarriage(String trainCode){
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        trainCarriageExample.setOrderByClause("`index` asc");
        trainCarriageExample.createCriteria().andTrainCodeEqualTo(trainCode);
        return trainCarriageMapper.selectByExample(trainCarriageExample);
    }

}
